package com.work.intelligent_transportation2.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.work.intelligent_transportation2.EnvironmentActivity;
import com.work.intelligent_transportation2.HomeActivity;
import com.work.intelligent_transportation2.R;
import com.work.intelligent_transportation2.Sign_inActivity;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import Api.Environment_api;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class EnvironmentWeatherSystemFragment extends Fragment {
    private boolean run = false;
    private Handler handler = new Handler();
    private String url = "http://121.9.253.237:8888/traffic/api/v2/get_all_sense";
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private OkHttpClient okHttp = new OkHttpClient();
    private String mParam1;
    private String mParam2;
    TextView tewendu;
    TextView teshidu;
    TextView teco2;
    TextView tedaolu;
    TextView tepm2_5;
    TextView teguanzhao;
    TextView kuwendu,kushidu,kuco2,kudaolu,kupm2_5,kuguanzhao;
    Button button;
    private SharedPreferences SP;
    public EnvironmentWeatherSystemFragment() {
    }

    public static EnvironmentWeatherSystemFragment newInstance(String param1, String param2) {
        EnvironmentWeatherSystemFragment fragment = new EnvironmentWeatherSystemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SP= getActivity().getSharedPreferences("Environ", Context.MODE_PRIVATE);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("UserName", "user1");
                String json = jsonObject.toString();
//MediaType对象包含了三种信息：type 、subtype以及charset，一般将这些信息传入parse()方法中，这样就可以解析出MediaType对象，比如 “text/x-markdown; charset=utf-8”
                MediaType Medipet = MediaType.Companion.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.create(json, Medipet);
                Request request = new Request.Builder().url(url).post(requestBody).build();
                Call call = okHttp.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        Log.i("tag", result);
                        Gson gson = new Gson();
                        Environment_api loginBean = gson.fromJson(result, Environment_api.class);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                String wendu= String.valueOf(loginBean.getHumidity());
                                String shidu= String.valueOf(loginBean.getTemperature());
                                String co2=String.valueOf(loginBean.getCo2());
                                String guanzhao= String.valueOf(loginBean.getLightIntensity());
                                String pm2_5= String.valueOf(loginBean.get_$Pm255());
                                int WD=SP.getInt("温度",0);
                                int SD=SP.getInt("湿度",0);
                                int c02=SP.getInt("co2",0);
                                int Gzhao=SP.getInt("光照",0);
                                int Pm2_5=SP.getInt("pm2_5",0);
                                if (loginBean.getRESULT().equals("S")){
                                    tewendu.setText(wendu);
                                    teshidu.setText(shidu);
                                    teco2.setText(co2);
                                    teguanzhao.setText(guanzhao);
                                    tepm2_5.setText(pm2_5);
                                    if (loginBean.get_$Pm255()>Pm2_5){
                                       kupm2_5.setBackgroundColor(0xffff0000);
                                    }else {
                                        kupm2_5.setBackgroundColor(0xff00ff00);
                                    }

                                    if (loginBean.getHumidity()>WD){
                                        kuwendu.setBackgroundColor(0xffff0000);
                                    }else {
                                        kuwendu.setBackgroundColor(0xff00ff00);
                                    }

                                    if (loginBean.getCo2()>c02){
                                        kuco2.setBackgroundColor(0xffff0000);
                                    }else {
                                        kuco2.setBackgroundColor(0xff00ff00);
                                    }

                                    if (loginBean.getTemperature()>SD){
                                        kushidu.setBackgroundColor(0xffff0000);
                                    }else {
                                        kushidu.setBackgroundColor(0xff00ff00);
                                    }

                                    if (loginBean.getLightIntensity()>Gzhao){
                                        kuguanzhao.setBackgroundColor(0xffff0000);
                                    }else {
                                        kuguanzhao.setBackgroundColor(0xff00ff00);
                                    }
                                }else if (loginBean.getRESULT().equals("F")){

                                }
                            }
                        });
                    }
                });
            }

        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    onCreate(null);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_environment_weather_system, container, false);
         tewendu= view.findViewById(R.id.tewendu);
         teshidu=view.findViewById(R.id.teshidu);
         teco2=view.findViewById(R.id.teco2);
         tedaolu=view.findViewById(R.id.tedaolu);
         tepm2_5=view.findViewById(R.id.tepm2_5);
         teguanzhao=view.findViewById(R.id.teguanzhao);
         button = view.findViewById(R.id.button);
        kuwendu=view.findViewById(R.id.kuawendu);
        kuco2=view.findViewById(R.id.kuco2);
        kushidu=view.findViewById(R.id.kushidu);
        kudaolu=view.findViewById(R.id.kudaolu);
        kupm2_5=view.findViewById(R.id.kupm2_5);
        kuguanzhao=view.findViewById(R.id.kuguanzhao);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), EnvironmentActivity.class);
                onStop();
                startActivity(intent);
            }
        });
        return view;
    }

}